package org.tloss.web.editor.model.command;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import org.tloss.web.editor.model.oper.math.MathOperand;
import org.tloss.web.editor.utils.Constants;

@XmlAccessorType(XmlAccessType.PROPERTY)
public class Assign extends SimpleCommand {

	private String name;
	private String type;
	private MathOperand value;

	@XmlAttribute
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@XmlAttribute
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@XmlElement
	public MathOperand getValue() {
		return value;
	}

	public void setValue(MathOperand value) {
		this.value = value;
	}

	@Override
	public String getLabel() {
		return "Assign";
	}

	@Override
	public String generate(int type, Object[] data) {
		if (Constants.CodeType.JAVA == type) {
			StringBuffer buffer = new StringBuffer();
			if (getType().equals(value.getType())) {
				buffer.append(getType()).append(" ").append(getName())
						.append("=").append(value.generate(type, data))
						.append(";");
			} else {
				buffer.append(getType()).append(" ").append(getName())
						.append("=Utils.getConvertor().convert(")
						.append(value.generate(type, data)).append(");");
			}
			return buffer.toString();
		}
		return "";
	}

}
